#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
GIS树控件

 - writer:linux_23; create: Jun 27 22:23:47 2008 ; version:1; 创建
"""


import wx
from wx.lib.mixins import treemixin
from Const import *
from conf import *
from panels import GetImageByName, GetImageDirPath

class DemoTreeMixin(treemixin.VirtualTree, treemixin.DragAndDrop, 
                    treemixin.ExpansionState):
    def __init__(self, *args, **kwargs):
        super(DemoTreeMixin, self).__init__(*args, **kwargs)
        self.CreateImageList()
    def CreateImageList(self):
        size = (32,32)
        self.imageList = wx.ImageList(*size)
        imgconf = GetImageByName("conf.png",GetImageDirPath(),size).ConvertToBitmap()
        imgcp = GetImageByName("compass.png",GetImageDirPath(),size).ConvertToBitmap()
        imgea = GetImageByName("earth.png",GetImageDirPath(),size).ConvertToBitmap()
        imgesri = GetImageByName("esri.png",GetImageDirPath(),size).ConvertToBitmap()
        imgmi = GetImageByName("mapinfo.png",GetImageDirPath(),size).ConvertToBitmap()
        imgtif = GetImageByName("geotiff.png",GetImageDirPath(),size).ConvertToBitmap()
        self.imageList.Add(imgconf)
        self.imageList.Add(imgcp)
        self.imageList.Add(imgea)
        self.imageList.Add(imgesri)
        self.imageList.Add(imgmi)
        self.imageList.Add(imgtif)
        self.AssignImageList(self.imageList)
    def OnDrop(self, dropTarget, dragItem):
        self.Drop(dragItem, dropTarget)

class VirtualTreeCtrl(DemoTreeMixin, wx.TreeCtrl):
    def GetItemId(self, index):
        item = self.GetRootItem()
        for i in index:
            for it in range(self.GetChildrenCount(item,False)):
                if it == 0:
                    tmpitem,cookie = self.GetFirstChild(item)
                    if i!=0:
                        continue
                    else:
                        break
                tmpitem = self.GetNextSibling(tmpitem)
                if it == i:
                    break
            item = tmpitem
        return item
    def OnGetChildrenCount(self, index):
        itemid = self.GetItemId(index)            
        #print self.GetItemText(itemid),index
        return self.GetChildrenCount(itemid,False)
    def OnGetItemText(self, index, column=0):
        itemid = self.GetItemId(index)            
        return self.GetItemText(itemid)
    def OnGetItemImage(self, index, which=wx.TreeItemIcon_Normal, column=0):
        itemid = self.GetItemId(index)            
        return self.GetItemImage(itemid, which)
    def Drop(self, item, tag):
        #print self.GetItemText(item)
        #print self.GetItemText(tag)
        par = self.GetItemParent(tag)
        if par.IsOk():
            if self.GetItemText(par)==MAP_TXT:
                txt = self.GetItemText(item)
                self.AppendItem(tag, txt.split('(')[0])
                GISDATA.reg_mapdata(self.GetItemText(tag), txt.split('(')[0])
                self.ExpandAllChildren(par)
        #self.RefreshItems()

